
const baseUrl = 'https://www.magicmanufactory.com';
// const baseUrl = 'https://ts1.magicmanuf/actory.com';
// const baseUrl = 'https://172.16.8.28:8080';


var  token = "";
//获取应用实例
var app = getApp()  


function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function dateFormat(date, format)//date格式化为字符串
{
  var o = {
    "M+": date.getMonth() + 1, //month
    "d+": date.getDate(),    //day
    "h+": date.getHours(),   //hour
    "m+": date.getMinutes(), //minute
    "s+": date.getSeconds(), //second
    "q+": Math.floor((date.getMonth() + 3) / 3),  //quarter
    "S": date.getMilliseconds() //millisecond
  }
  if (!format) {
    format = "yyyy-MM-dd hh:mm:ss";
  }
  if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o) if (new RegExp("(" + k + ")").test(format))
    format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
  return format;
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function moneyPaddingZero(money, sum) {
  let str = '' + money;
  if (str.indexOf('.') == -1) {
    str += '.00000';
  } else {
    str += '00000';
  }
  str = str.slice(0, str.indexOf('.') + sum + 1);
  return str;
}

/// 比较两个对象相等
function cmp(x, y) {
  // If both x and y are null or undefined and exactly the same
  if (x === y) {
    return true;
  }

  // If they are not strictly equal, they both need to be Objects
  if (!(x instanceof Object) || !(y instanceof Object)) {
    return false;
  }

  // They must have the exact same prototype chain, the closest we can do is
  // test the constructor.
  if (x.constructor !== y.constructor) {
    return false;
  }

  for (var p in x) {
    // Inherited properties were tested using x.constructor === y.constructor
    if (x.hasOwnProperty(p)) {
      // Allows comparing x[ p ] and y[ p ] when set to undefined
      if (!y.hasOwnProperty(p)) {
        return false;
      }

      // If they have the same strict value or identity then they are equal
      if (x[p] === y[p]) {
        continue;
      }

      // Numbers, Strings, Functions, Booleans must be strictly equal
      if (typeof (x[p]) !== "object") {
        return false;
      }

      // Objects and Arrays must be tested recursively
      if (!Object.equals(x[p], y[p])) {
        return false;
      }
    }
  }

  for (p in y) {
    // allows x[ p ] to be set to undefined
    if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
      return false;
    }
  }
  return true;
}

// 手机号检查
function checkPhoneFromString(str) {
  let num = parseInt(str);
  return (/^1[34578]\d{9}$/.test(num));
}

// 判断传入的字符串是否为空
function isEmpty(str) {
  return str == null || str == '';
}

// 判断obj是否为空
function isEmptyObject(obj) {
  for (var n in obj) {
    return false;
  }
  return true;
}

// 是否为int
function isInt(n) {
  return Number(n) === n && n % 1 === 0;
}

// 是否为float
function isFloat(n) {
  return Number(n) === n && n % 1 !== 0;
}

function request(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url;

  let mohuanUser = getApp().globalData.mohuanUser
 
  
  
  if (mohuanUser) {
    token = mohuanUser["X-AUTH-TOKEN"]
  }
  console.log("token==================>" + token)
  wx.request({

    url: murl,
    method: 'GET',
    header: {
      'Accept': 'application/json;charset=UTF-8',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model,
      'Content-Encoding': 'gzip',
      'X-AUTH-TOKEN': token
    },
    success: function (res) {
      console.log("输出我的");
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}

function testGet(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
    if (typeof success != 'function' || typeof fail != 'function') {
        return
    }
    var murl = baseUrl + url;

    let mohuanUser = getApp().globalData.mohuanUser

    if (mohuanUser) {
        token = mohuanUser["X-AUTH-TOKEN"]
    }
    console.log("token==================>" + token)
    wx.request({
        url: murl,
        method: 'GET',
        header: {
            'Accept': 'application/json;charset=UTF-8',
            // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
            'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model,
            'Content-Encoding': 'gzip',
            'X-AUTH-TOKEN': token
        },
        success: function (res) {
            console.log("输出我的");
            if (res.data.returnCode == 200) {
                success(res.data)
            } else {
                fail(res.data.reason)
            }
        },
        fail: function (res) {
            fail(res)
        }
    })
}
//11月25号改过header Identify参数
function postRequest(url, mdata, success, fail) {
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url
  let mohuanUser = getApp().globalData.mohuanUser
  let token
  if (mohuanUser) {
    token = mohuanUser["X-AUTH-TOKEN"]
  }
  var imageid = getApp().globalData.imageRequestId ? getApp().globalData.imageRequestId : "";
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (imageid.length) {
    imageid = 'imageRequestId=' + imageid;
  }
  console.log('输出我的image' + imageid);
  // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu  
  wx.request({
    url: murl,
    method: 'POST',
    header: {
      'Accept': 'application/json',
      'C2M-Identify':  'Platform: MiniProgram, APP Version:2.08 OS Version:'+system+',Device Type:'+model,
      'Content-Encoding': 'gzip',
      'X-AUTH-TOKEN': token,
      'Cookie': imageid
    },
    data: mdata,
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res)
      }
    },
    fail: function (res) {
        fail(res)
       
    }
  })
}
function testPost(url, mdata, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
    if (typeof success != 'function' || typeof fail != 'function') {
        return
    }
    var murl = testUrl + url
    let mohuanUser = getApp().globalData.mohuanUser
    let token
    if (mohuanUser) {
        token = mohuanUser["X-AUTH-TOKEN"]
    }
    var imageid = getApp().globalData.imageRequestId ? getApp().globalData.imageRequestId : "";
    if (imageid.length) {
        imageid = 'imageRequestId=' + imageid;
    }
    console.log('输出我的image' + imageid);
    wx.request({
        url: murl,
        method: 'POST',
        header: {
            'Accept': 'application/json',
            // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
            'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08 OS Version:' + system + ',Device Type:' + model,
            'Content-Encoding': 'gzip',
            'X-AUTH-TOKEN': token,
            'Cookie': imageid
        },
        data: mdata,
        success: function (res) {
            if (res.data.returnCode == 200) {
                success(res.data)
            } else {
                fail(res.data.reason)
            }
        },
        fail: function (res) {
            fail('网络错误');
        }
    })
}
function deleteRequest(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url
  
  wx.request({

    url: murl,
    method: 'DELETE',
    header: {
      'Accept': 'application/json',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model,
      'Content-Encoding': 'gzip',
      'X-AUTH-TOKEN': wx.getStorageSync('userToken')
    },
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}

function json2Form(json) {
    var str = [];
    for (var p in json) {
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
    }
    return str.join("&");

} 
function deleteDataRequest(url, mdata,success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
    if (typeof success != 'function' || typeof fail != 'function') {
        return
    }
    var murl = baseUrl + url;
    let mohuanUser = getApp().globalData.mohuanUser
    let token
    if (mohuanUser) {
        token = mohuanUser["X-AUTH-TOKEN"]
    }
    var imageid = getApp().globalData.imageRequestId ? getApp().globalData.imageRequestId : "";
    if (imageid.length) {
        imageid = 'imageRequestId=' + imageid;
    }
    console.log('输出我的image' + imageid);
    wx.request({
        url: murl,
        method: 'DELETE',
        data: mdata,
        header: {
            'Accept': 'application/json',
            // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
            'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model,
            'Content-Encoding': 'gzip',
            'X-AUTH-TOKEN': token,
            'Cookie': imageid
        },
        success: function (res) {
            if (res.data.returnCode == 200) {
                success(res.data)
            } else {
                fail(res.data.reason)
            }
        },
        fail: function () {
            fail('网络错误')
        }
    })
}
function getRegisterCode(success, fail) {
  var abosulteUrl = baseUrl + '/c2mwebservice/api/v2/public/captcha/image-stream/reg';
  wx.request({
    url: abosulteUrl,
    data: {},
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      success(res);
    },
    fail: function (reason) {
      fail(reason);
      // fail
    },
    complete: function () {
      // complete
    }
  })
  //   wx.request({
  //      url: abosulteUrl,
  //      method: 'GET',
  //     success: function(res ) {
  //        // success(res);
  //     },
  //     fail: function() {
  //       fail('网络错误')
  //     }
  //   })
}

//定制页面
// 获取场合列表
function getSenceList(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url

  wx.request({

    url: murl,
    method: 'get',
    header: {
      // 'Accept': 'application/json',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      // 'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model,'Device Type':xiaochengxu,
      // 'Content-Encoding': 'gzip',
      // 'X-AUTH-TOKEN': wx.getStorageSync('userToken')
    },
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}
// 获取需求id
function getrequirementId(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url

  wx.request({

    url: murl,
    method: 'get',
    header: {
      // 'Accept': 'application/json',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      // 'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model,'Device Type':xiaochengxu,
      // 'Content-Encoding': 'gzip',
      // 'X-AUTH-TOKEN': wx.getStorageSync('userToken')
    },
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}
// 提交需求接口
function postrequirementInput(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url
  let mohuanUser = getApp().globalData.mohuanUser
  let token
  if (mohuanUser) {
    token = mohuanUser["X-AUTH-TOKEN"]
  }
  wx.request({

    url: murl,
    method: 'POST',
    header: {
      'Accept': 'application/json',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      // 'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model,'Device Type':xiaochengxu,
      'Content-Encoding': 'gzip',
      'X-AUTH-TOKEN': token
    },
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}

// 获取品类接口
function getcateList(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url

  wx.request({

    url: murl,
    method: 'get',
    header: {
      // 'Accept': 'application/json',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      // 'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model,'Device Type':xiaochengxu,
      // 'Content-Encoding': 'gzip',
      // 'X-AUTH-TOKEN': wx.getStorageSync('userToken')
    },
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}
// 获取个人推广二维码
function getQrcodeImg(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url
  let mohuanUser = getApp().globalData.mohuanUser
  let token
  if (mohuanUser) {
    token = mohuanUser["X-AUTH-TOKEN"]
  }
  wx.request({

    url: murl,
    method: 'get',
    header: {
      'Accept': 'application/json',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      // 'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model, 'Device Type': xiaochengxu,
      // 'Content-Encoding': 'gzip',
      'X-AUTH-TOKEN': token
    },
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}
//   线下订单接口
function underLineOrder(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url
  let mohuanUser = getApp().globalData.mohuanUser
  let token
  if (mohuanUser) {
    token = mohuanUser["X-AUTH-TOKEN"]
  }
  wx.request({

    url: murl,
    method: 'get',
    header: {
      'Accept': 'application/json',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      // 'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model, 'Device Type': xiaochengxu,
      // 'Content-Encoding': 'gzip',
      'X-AUTH-TOKEN': token
    },
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}
// 获取消息列表
function postNewsList(url, success, fail) {
  var system = getApp().globalData.deviceInfo.system;//手机系统版本
  var model = getApp().globalData.deviceInfo.model;//手机型号
  if (typeof success != 'function' || typeof fail != 'function') {
    return
  }
  var murl = baseUrl + url
  let mohuanUser = getApp().globalData.mohuanUser
  let token
  if (mohuanUser) {
    token = mohuanUser["X-AUTH-TOKEN"]
  }
  wx.request({

    url: murl,
    method: 'post',
    header: {
      'Accept': 'application/json',
      // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
      // 'C2M-Identify': 'Platform: MiniProgram, APP Version:2.08  OS Version:' + system + ',Device Type:' + model, 'Device Type': xiaochengxu,
      // 'Content-Encoding': 'gzip',
      'X-AUTH-TOKEN': token
    },
    success: function (res) {
      if (res.data.returnCode == 200) {
        success(res.data)
      } else {
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail('网络错误')
    }
  })
}


module.exports = {
  getRegisterCode: getRegisterCode,
  postRequest: postRequest,
  testPost: testPost,
  request: request,
  deleteRequest: deleteRequest,
  deleteDataRequest: deleteDataRequest,
  formatTime: formatTime,
  dateFormat: dateFormat,
  cmp: cmp,
  json2Form: json2Form,
  checkPhoneFromString: checkPhoneFromString,
  isEmpty: isEmpty,
  isEmptyObject: isEmptyObject,
  isInt: isInt,
  isFloat: isFloat,
  moneyPaddingZero: moneyPaddingZero,
  baseUrl: baseUrl,
  token:token,
  testGet: testGet,
 //在线定制助理模块
  getSenceList: getSenceList, //场合列表
  getrequirementId: getrequirementId,//需求ID
  postrequirementInput: postrequirementInput,//提交需求
  getcateList: getcateList, //获取品类
  getQrcodeImg: getQrcodeImg,//获取个人推广二维码
  

  // 个人中心
  underLineOrder: underLineOrder,//线下订单
  postNewsList: postNewsList


 
}
